<template>
  <div class="login">
    <el-card>
      <img class="img" src="@/assets/logo.png" alt="" />
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item prop="isCheck">
          <el-checkbox v-model="form.isCheck"
            >我已阅读并同意[用户协议]和[隐私条款]</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入登录接口的api接口
import { authorizationsPost } from '@/api/index.js'
// 导入token方法
import { setItem } from '../../utils/author.js'
export default {
  data () {
    return {
      form: {
        mobile: '13200000000',
        code: '246810',
        isCheck: []
      },
      rules: {
        mobile: [
          { required: true, message: '请输入正确手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '长度位11位手机号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const res = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (res.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            }
          }
        ],
        code: [
          { required: true, message: '请输入正确验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '长度位6位验证码', trigger: 'blur' }
        ],
        isCheck: [{ required: true, message: '请勾选', trigger: 'change' }]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async res => {
        if (res) {
          // 1.axios请求 不使用.then.catch方法 统一使用async/await
          const res = await authorizationsPost(this.form)
          console.log(res)
          // 2.请求成功需要携带token 因为多次携带需要定义在utils中
          // localStorage.setItem('Token75', res.data.token)
          setItem(res.data.token)
          this.$message.success('验证成功')
          // 4.成功后步骤(创建首页页面/配置路由/跳转)
          this.$router.push('/home')

          // 1.axios请求
          // authorizationsPost(this.form).then(res => {
          //   console.log(res)
          // })
          // this.$message.success('验证成功')
          // } else {
          // this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background-image: url(~@/assets/login.png);
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    width: 460px;
    .img {
      width: 200px;
      height: 60px;
      display: block;
      margin: 0 auto;
    }
    .btn {
      width: 100%;
    }
    // 加了scopde 看得见的标签就管的到 看不到就管不到
    ::v-deep .el-card__body {
      padding: 20px 40px;
    }
  }
}
</style>
